package com.action;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.beans.Manufacturer;
import com.hibernate.HibernateUtil;
import com.opensymphony.xwork2.ActionSupport;



public class ManufacturerAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private Manufacturer manufacturer;
	
	
	public String execute() {
		String strID = ServletActionContext.getRequest().getParameter("Selectedid");
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		//this is for the case where the Edit Manufacturer function is used in the search-result page, begin:
		String returntosearch=ServletActionContext.getRequest().getParameter("returntosearch");
		if(returntosearch!=null){
			ServletActionContext.getRequest().setAttribute("returntosearch", "true");
		}
		//end
		
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			manufacturer = (Manufacturer) session.get(Manufacturer.class, Integer.parseInt(strID));
			transaction.commit();}
		catch(HibernateException e){
			transaction.rollback();
			e.printStackTrace();}
		finally{
			session.close();}
		//ServletActionContext.getRequest().getSession().setAttribute("Selectedid", ServletActionContext.getRequest().getParameter("Selectedid"));
		return SUCCESS;
	}
	
	
	// OK
	public String deleteManufacturerList() {
		String[] strIDList = ServletActionContext.getRequest().getParameterValues("IdCheck");
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		transaction = session.beginTransaction();
		try {
			if(strIDList != null) {
				for(int i = 0; i < strIDList.length; i++) {
					manufacturer = (Manufacturer) session.get(Manufacturer.class, Integer.parseInt(strIDList[i]));
					session.delete(manufacturer);
				}
			
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}
		finally {
			session.close();
		}
		
		return SUCCESS;
	}
	
	// OK
	public String editManufacturer() {
		
		String strID = ServletActionContext.getRequest().getParameter("id");
		String addr = ServletActionContext.getRequest().getParameter("addr");
		String phone = ServletActionContext.getRequest().getParameter("telephone");
		String des = ServletActionContext.getRequest().getParameter("des");
		String name = ServletActionContext.getRequest().getParameter("name");
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try{
		
			transaction = session.beginTransaction();
		
			manufacturer = (Manufacturer) session.get(Manufacturer.class, Integer.parseInt(strID));
			manufacturer.setAddr(addr);
			manufacturer.setDes(des);
			manufacturer.setTelephone(phone);
			manufacturer.setName(name);
		
			session.update(manufacturer);
			transaction.commit();}
		catch(HibernateException e){
			transaction.rollback();
			e.printStackTrace();}
		finally{
			session.close();}
		return SUCCESS;
	}
	
	public String addManufacturer() {
		String addr = ServletActionContext.getRequest().getParameter("addr");
		String phone = ServletActionContext.getRequest().getParameter("telephone");
		String des = ServletActionContext.getRequest().getParameter("des");
		String name = ServletActionContext.getRequest().getParameter("name");
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
		
			manufacturer = new Manufacturer();
			manufacturer.setAddr(addr);
			manufacturer.setDes(des);
			manufacturer.setTelephone(phone);
			manufacturer.setName(name);
		
			session.save(manufacturer);
			transaction.commit();}
		catch(HibernateException e){
			transaction.rollback();
			e.printStackTrace();}
		finally{
			session.close();}
		return SUCCESS;
	}

	public Manufacturer getManufacturer() {
		return manufacturer;
	}

	public void setManufacturer(Manufacturer manufacturer) {
		this.manufacturer = manufacturer;
	}

}
